package cn.zktr.Personnel.model.service.ouyangpeng;

import java.io.File;
import java.io.FileOutputStream;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional(rollbackFor=Exception.class)
public class StorePicturesService {
	/*** 
	* 新增文件 
	* @param file 
	* @return 
	*/ 
	public String saveFile(MultipartFile file) {

	try { 
	// 文件保存路径 
	String filePath = "E:/Personnel/Personnel/src/main/webapp/static/oyp/images/";//映射的地址
	
	//String filePath = request.getSession().getServletContext().getRealPath("upload/");本地项目路径

	String filename = file.getOriginalFilename();//获取file图片名称
	String result=uploadFile(file.getBytes(), filePath, filename);
	return result;  

	} catch (Exception e) { 
	} 

	return null;
	} 

	public static String uploadFile(byte[] file, String filePath, String fileName) throws Exception { 
	File targetFile = new File(filePath); 
	if(!targetFile.exists()){ 
	targetFile.mkdirs(); 
	} 
	FileOutputStream out = new FileOutputStream(filePath+fileName);
	out.write(file);
	out.flush();
	out.close();
	System.err.println(fileName);
	System.err.println(filePath+fileName);
	return "static\\imgs\\"+fileName;
	}
}
